package chat.fragments;

import android.app.DialogFragment;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.NumberKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;
import com.google.gson.Gson;

import chat.network.http.AddFriend;
import inter.Delegate;
import utils.TimeUtil;

/**
 * 添加好友
 * Created by 凯 on 2015/10/26.
 */
public class AddFriendFragment extends DialogFragment  {
    public static final String TAG_ADDFRIEND = "dialog_addfiend";
    TextView tv_alert;
    EditText et_account;
    EditText et_message;
    View v_add;

    Delegate getAddFriendResponese = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            AddFriend.ResponseAddContact responseAddContact = gson.fromJson(response, AddFriend.ResponseAddContact.class);

            if (responseAddContact.getStatus().equals(AddFriend.AddContactStatus.SUCCESS))
            {
                Toast.makeText(MyApplication.share(),"好友请求成功",Toast.LENGTH_SHORT).show();
                dismiss();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };

    AddFriend addFriend = new AddFriend();
    {
        addFriend.setDelegate(getAddFriendResponese);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_addfriend, container, false);
        initview(view);
        return view;
    }

    View.OnClickListener btnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_add) {
                addnewFriend();
            }
        }
    };

    // 添加好友
    private void addnewFriend() {
        String str_account = et_account.getText().toString();
        String str_message = et_message.getText().toString();

        if (str_account.length() < 1)
        {
            Toast.makeText(getActivity(),"好用ID不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (str_account.equals(MyApplication.getOwnUser().getUser_ID()))
        {
            Toast.makeText(getActivity(),"不能添加自己为好友",Toast.LENGTH_SHORT).show();
        }

        // 若已经是好友
//        if (LoginModel.isUserExists(str_account))
//        {
//            tv_alert.setText("已经是好友了！");
//            return;
//        }


        if (str_account.length() > 12) {
            return;
        }

        if (str_account.length() > 50) {
            return;
        }

        AddFriend.SubscripContact subscripContact = new AddFriend.SubscripContact();
        subscripContact.setSubscrip_from_Group("");
        subscripContact.setSubscrip_from_ID(MyApplication.getOwnUser().getUser_ID());
        subscripContact.setSubscrip_to_ID(str_account);
        subscripContact.setSubscrip_Desc(str_message);
        subscripContact.setSubscrip_Time(TimeUtil.currentTime());
        addFriend.setSubscripContact(subscripContact);
        addFriend.add();
    }

    // 初始化控制
    private void initview(View view) {
        tv_alert = (TextView) view.findViewById(R.id.id_tv_alert);
        et_account = (EditText) view.findViewById(R.id.id_et_account);
        et_message = (EditText) view.findViewById(R.id.id_et_message);

        v_add = (View) view.findViewById(R.id.id_dialog_bottom);
        v_add.setOnClickListener(btnClick);


        // 账号只能是数字的组合
       et_account.setKeyListener(new NumberKeyListener() {
           @Override
           protected char[] getAcceptedChars() {
               StringBuffer sb = new StringBuffer();
               for(int i=48;i<=57;i++)
               {
                   sb.append((char)i);
               }

//               for (int i=65;i<=90;i++)
//               {
//                   sb.append((char)i);
//               }
//
//               for(int i=97;i<=122;i++)
//               {
//                   sb.append((char)i);
//               }

               return sb.toString().toCharArray();
           }

           @Override
           public int getInputType() {
               return InputType.TYPE_MASK_VARIATION;
           }
       });
    }


}
